﻿namespace _14最长公共前缀
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] ss = { "flower", "flow", "flight" };

            Console.WriteLine(Solution.LongestCommonPrefix(ss));
        }
    }
    public class Solution
    {
        public static string LongestCommonPrefix(string[] strs)
        {
            string perfix = strs[0];
            for (int i = 1; i < strs.Length; i++)
            {
                perfix = longestCommonPrefix(perfix, strs[i]);
                if (perfix == "")
                {
                    break;
                }
            }
            return perfix;

        }
        public static String longestCommonPrefix(String str1, String str2)
        {
            //最长前缀长度取决于最短字符串
            int maxLen = Math.Min(str1.Length, str2.Length);
            int index = 0;
            while (index < maxLen && (str1[index] == str2[index]))
            {
                index++;
            }
            return str1.Substring(0, index);
        }
    }
}
